﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ConvexPolygon
{
    public partial class PointsFrm : Form
    {
        PointF[] pointsData;
        public PointsFrm(ref PointF[] p)
        {
            InitializeComponent();
            pointsData = p;
            dataGW.DataSource = p;
            dataGW.MultiSelect = false;
        }

        private void dataGW_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            MessageBox.Show("blogas formatas");
        }

        private void dataGW_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            DataGridView dgv = sender as DataGridView;
            if (e.ColumnIndex == 0)
            {
                pointsData[e.RowIndex].X = float.Parse(dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].EditedFormattedValue.ToString());
            }
            if (e.ColumnIndex == 1)
            {
                pointsData[e.RowIndex].Y = float.Parse(dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].EditedFormattedValue.ToString());
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            PointF[] temp = new PointF[pointsData.Length + 1];
            int insertIndex = dataGW.SelectedCells[0].RowIndex+1;
            Array.Copy(pointsData, temp, insertIndex);
            Array.Copy(pointsData, insertIndex, temp, insertIndex + 1, temp.Length - insertIndex - 1);
            pointsData = temp;

            dataGW.DataSource = pointsData;
            dataGW.Refresh();
            dataGW.Rows[insertIndex].Cells[0].Selected = true;

            //MessageBox.Show(dataGW.SelectedCells[0].RowIndex.ToString());

        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            int deleteIndex = dataGW.SelectedCells[0].RowIndex;

            if (pointsData.Length - 1 != 0)
            {
                PointF[] temp = new PointF[pointsData.Length - 1];
                Array.Copy(pointsData, temp, deleteIndex);
                Array.Copy(pointsData, deleteIndex+1, temp, deleteIndex, temp.Length - deleteIndex);
                pointsData = temp;
            }
            else
            {
                pointsData = new PointF[1]{new PointF(0f,0f)};
            }
            dataGW.DataSource = pointsData;
            dataGW.Refresh();
            if (deleteIndex < pointsData.Length - 1)
            {
                dataGW.Rows[deleteIndex].Cells[0].Selected = true;
            }
            else
            {
                dataGW.Rows[pointsData.Length - 1].Cells[0].Selected = true;
            }

        }

        private void btnUp_Click(object sender, EventArgs e)
        {
            int selectedIndex = dataGW.SelectedCells[0].RowIndex;
            if (selectedIndex > 0)
            {
                PointF temp;
                temp = pointsData[selectedIndex];
                pointsData[selectedIndex] = pointsData[selectedIndex - 1];
                pointsData[selectedIndex - 1] = temp;
                dataGW.DataSource = pointsData;
                dataGW.Refresh();
                dataGW.Rows[selectedIndex - 1].Cells[0].Selected = true;
            }
        }

        private void btnDown_Click(object sender, EventArgs e)
        {
            int selectedIndex = dataGW.SelectedCells[0].RowIndex;
            if (selectedIndex < pointsData.Length-1)
            {
                PointF temp;
                temp = pointsData[selectedIndex];
                pointsData[selectedIndex] = pointsData[selectedIndex + 1];
                pointsData[selectedIndex + 1] = temp;
                dataGW.DataSource = pointsData;
                dataGW.Refresh();
                dataGW.Rows[selectedIndex + 1].Cells[0].Selected = true;
            }
        }
    }
}
